package com.mashibing.juc.c_019;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * LockSupport控制线程停车
 * @author: Yuanzt
 * @Date: 2021/3/29
 */
public class T13_TestLockSupport {

    public static void main(String[] args) {
        Thread t = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                if (i == 5) LockSupport.park();//控制线程停车

                if (i == 8) LockSupport.park();//两次线程停车，LockSupport.unpark(t)无法解除线程停车
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();

        try {
            TimeUnit.SECONDS.sleep(8);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("after 2 seconds");
        LockSupport.unpark(t);
//        LockSupport.unpark(t);
    }
}
